//The MIT License
//
//Copyright (c) 2009 nodchip
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.
package tv.dyndns.kishibe.client.packet;

import java.io.Serializable;

import tv.dyndns.kishibe.client.Constant;
import tv.dyndns.kishibe.client.game.GameMode;

public class PacketRoomKey implements Serializable {
	private static final long serialVersionUID = -8999699065125269820L;
	public GameMode gameMode;
	public String name;
	public int genre;
	public int type;

	public void set(GameMode gameMode, String name, int genre, int type) {
		this.gameMode = gameMode;
		this.name = name;
		this.genre = gameMode == GameMode.event ? genre : 0;
		this.type = gameMode == GameMode.event ? type : 0;
	}

	@Override
	public int hashCode() {
		return (gameMode != null ? gameMode.hashCode() : 0) + (name != null ? name.hashCode() : 0) + genre + type;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return false;
		}

		if (!(obj instanceof PacketRoomKey)) {
			return false;
		}

		final PacketRoomKey rh = (PacketRoomKey) obj;
		boolean result = true;

		result &= gameMode == null ? rh.gameMode == null : gameMode.equals(rh.gameMode);
		result &= name == null ? rh.name == null : name.equals(rh.name);
		result &= genre == rh.genre;
		result &= type == rh.type;

		return result;
	}

	public String toString() {
		final StringBuffer sb = new StringBuffer();
		sb.append(name);
		sb.append(" ");
		for (int genre = 0; genre < Constant.NUMBER_OF_GENRE; ++genre) {
			if ((this.genre & (1 << genre)) != 0) {
				sb.append(Constant.INITIAL_OF_GENRE[genre]);
			}
		}
		for (int type = 0; type < Constant.NUMBER_OF_TYPE_WITH_RANDOM; ++type) {
			if ((this.type & (1 << type)) != 0) {
				sb.append(Constant.INITIAL_OF_TYPE[type]);
			}
		}
		return sb.toString();
	}
}
